/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CGFormEdit.java
 *
 * Created on 20-jul-2011, 17:38:15
 */
package pfc.gui;

import java.awt.Component;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import pfc.arbol.FormTreeNode;
import pfc.comun.IDEpfc;
import pfc.interfaces.Salvable;
import pfc.modelocg.CGField;
import pfc.modelocg.CGForm;
import pfc.modelocg.CGForm.FormType;
import pfc.modelocg.CGModel;
import pfc.modelocg.CGModelIO;

/**
 *
 * @author xandru
 */
public class CGFormEdit extends Editor implements Salvable {

    
    private final FormTreeNode _nodo;

    /** Creates new form CGFormEdit */
    public CGFormEdit(FormTreeNode n) {
        this._nodo = n;
        initComponents();
        loadData();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Campo", "Visible", "Editable", "Busqueda"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("jLabel1");

        jButton1.setText("Aplicar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cerrar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(343, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(115, 115, 115)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap(131, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(25, 25, 25))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        save();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.setVisible(false);
        this.getParent().remove(this);
        IDEpfc.app.getIDE().removeChanged(this);
    }//GEN-LAST:event_jButton2ActionPerformed

    public void save() {
        CGModel m = this._nodo.getCGModel();
        CGForm cgForm = this._nodo.getCGForm();
        String fichero = this._nodo.getCodeGenerationFile();

        cgForm.setNumberOfLines(1);
        cgForm.setNumberOfColumns(1);


        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        for (int row = 0; row < model.getRowCount(); row++) {
            String campo = (String) model.getValueAt(row, 0);
            Boolean visible = (Boolean) model.getValueAt(row, 1);
            Boolean editable = (Boolean) model.getValueAt(row, 2);
            //Boolean crearSelect = (Boolean) model.getValueAt(row, 3);
            Boolean crearSelect = new Boolean(false);

            CGField field = cgForm.getField(campo);
            field.setVisible(visible);
            field.setEditable(editable);
            field.setCreateSelect(crearSelect);
        }
        CGModelIO.model2XML(m, fichero);

        IDEpfc.logTime("Propiedades del formulario " + cgForm.getName() + " guardadas");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private void loadData() {
        Boolean pedirColumnas = false;
        Boolean pedirLineas = false;
        Boolean pedirCampoEditable = false;
        jLabel1.setText(this._nodo.getCGForm().getName() + " : " + this._nodo.getCGForm().getType());
        switch (this._nodo.getCGForm().getType()) {
            case EditForm:
                pedirColumnas = true;
                pedirCampoEditable = true;
                break;
            case LinesForm:
                pedirLineas = true;
                break;
            case SearchForm:
                break;
        }

        DefaultTableModel m = crearTableModel(pedirCampoEditable);
        jTable1.setModel(m);
                
        for (CGField field : this._nodo.getCGForm().getFields()) {

            m.addRow(new Object[]{field.getName(), field.isVisible(), field.isEditable(), field.createSelect()});
        }
               
    }

    private DefaultTableModel crearTableModel(final Boolean pedirCampoEditable) {

        DefaultTableModel m = new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                //new String[]{"Campo", "Visible", "Editable", "Combobox"}) {
                new String[]{"Campo", "Visible", "Editable"}) {
            //Class[] types = new Class[]{java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class};
            Class[] types = new Class[]{java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class};
            //boolean[] canEdit = new boolean[]{false, true, pedirCampoEditable, true};
            boolean[] canEdit = new boolean[]{false, true, pedirCampoEditable};
            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        };



        return m;
    }
}
